﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CSharpDay07__Singleton_.Models;
using System.IO;

namespace CSharpDay07__Singleton_.Controllers
{
    public class HomeController : Controller
    {
        DB db = DB.Instance;
        public ActionResult Index()
        {
            return View(db.Movies);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpGet]
        public ActionResult EditMovie(int Id)
        {
            return View(db.Movies.FirstOrDefault(x => x.Id == Id));
        }
        [HttpPost]
        public ActionResult EditMovie(int Id, string ImageUrl, string Title, MovieRating Rating)
        {

            var Terminator3 = db.Movies.FirstOrDefault(x => x.Id == Id);
            Terminator3.ImageUrl = ImageUrl;
            Terminator3.Title = Title;
            Terminator3.Rating = Rating;



            return
                RedirectToAction("Index");
        }

        public ActionResult AddMovie()
        {

            return View(new Movie());
        }
        [HttpPost]

        public ActionResult AddMovie(string ImageUrl, string Title, MovieRating Rating)
        {
            Movie mymovie = new Movie();
            mymovie.ImageUrl = ImageUrl;
            mymovie.Title = Title;
            mymovie.Rating = Rating;
            mymovie.Id = db.Movies.LastOrDefault().Id;
            mymovie.Id += 1;
            db.Movies.Add(mymovie);


            return
                RedirectToAction("Index");

        }
        public ActionResult DeleteMovie(int Id)
        {
            Movie removeMovie = db.Movies.FirstOrDefault(x => x.Id == Id);
            db.Movies.Remove(removeMovie);

            return RedirectToAction("Index");
        }


    }
}